package com.bobby.utils;

import com.bobby.pojo.Cart;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Description TODO
 * @Date 2020/8/14 14:48
 * @Created wenjunpei
 */
public class WebUtils {

    public static  <T> T copyParamToBean(Map properties , Class<T> clazz) throws Exception {
        T bean = clazz.newInstance();
        BeanUtils.populate(bean,properties);
        return bean;
    }

    public static Cart getCart(HttpServletRequest request) {
        Cart cartFromSession = (Cart) request.getSession().getAttribute("cart");
        if(cartFromSession == null) {
            cartFromSession = new Cart();
            request.getSession().setAttribute("cart",cartFromSession);
        }
        return cartFromSession;
    }
}
